package earogya.ehr.patient_details.messages;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.v251.datatype.TS;
import ca.uhn.hl7v2.model.v251.datatype.XAD;
import ca.uhn.hl7v2.model.v251.datatype.XPN;
import ca.uhn.hl7v2.model.v251.message.ADT_A01;
import ca.uhn.hl7v2.model.v251.segment.PD1;
import ca.uhn.hl7v2.model.v251.segment.PID;
import earogya.ehr.patient_details.HibernateManager;
import earogya.ehr.patient_details.Patient;
import earogya.ehr.patient_details.PatientAddress;


public class ADT_A08_Handler {
	private Patient patient=new Patient();
	private PatientAddress pAddress;
	HibernateManager hibernateManager=new HibernateManager();

	private ADT_A01 adtMsg;
	public ADT_A08_Handler(ADT_A01 adtMsg){
		this.adtMsg=adtMsg;
	}
	
	public String manageMessage() throws HL7Exception, IOException{
		String ack=adtMsg.generateACK().encode();
		this.updatePatientDetails();
		this.updatePatientAddressDetails();
		return ack;
	}
	
	public void updatePatientDetails() throws HL7Exception{
		PID pid=adtMsg.getPID();
		PD1 pd1=adtMsg.getPD1();
		patient.setPatientId(Integer.parseInt(pid.getPatientID().getIDNumber().getValue()));
		System.out.print(patient.getPatientId());
		XPN pName=pid.getPatientName(0);
		if(pName.getPrefixEgDR().getValue()==null){}else{patient.setPrefix(pName.getPrefixEgDR().getValue());}
		if(pName.getGivenName().getValue()==null){}else{patient.setGivenName(pName.getGivenName().getValue());}
		if(pName.getFamilyName().getSurname().getValue()==null){}else{patient.setFamilyName(pName.getFamilyName().getSurname().getValue());}
		if(pid.getPatientIdentifierList(0).getIDNumber().getValue()==null){}else{patient.setNicNo(pid.getPatientIdentifierList(0).getIDNumber().getValue());}
		//System.out.print(patient.getNicNo());
		if(pid.getAdministrativeSex().getValue()==null){}else{patient.setGender(pid.getAdministrativeSex().getValue());}
		if(pid.getDateTimeOfBirth().getTime().getValue()==null){}else{patient.setBirthTime(pid.getDateTimeOfBirth().getTime().getValueAsDate());}
		if(pid.getBirthPlace().getValue()==null){}else{patient.setBirthPlaceAddress(pid.getBirthPlace().toString());}
		if(pid.getMultipleBirthIndicator().getValue()==null){}else{patient.setMultipleBirthIndex(Short.valueOf(pid.getMultipleBirthIndicator().getValue()));}
		if(pid.getBirthOrder()==null){patient.setMultipleBirthOrderNumber(Short.valueOf(pid.getBirthOrder().getValue()));}
		if(pd1.getOrganDonorCode().getValue()==null){}else{patient.setOrganDonorIndex(Integer.valueOf(pd1.getOrganDonorCode().getValue()));}
		if(pid.getMaritalStatus().getComponent(0).toString()==null){}else{patient.setMaritialStatus(pid.getMaritalStatus().getComponent(0).toString());}
		if(pd1.getLivingArrangement().getValue()==null){}else{patient.setLivingArrangementId(Integer.valueOf(pd1.getLivingArrangement().getValue()));}
		if(pid.getReligion().getComponent(0).toString()==null){}else{patient.setReligiousAffliationId(pid.getReligion().getComponent(0).toString());}
		if(pid.getEthnicGroup(0).getComponent(0).toString()==null){}else{patient.setEthnicGroupId(pid.getEthnicGroup(0).getComponent(0).toString());}
		if(pid.getPatientDeathIndicator().getValue()==null){}else{patient.setDeceasedIndex(Boolean.parseBoolean(pid.getPatientDeathIndicator().getValue()));}
		if(pid.getPatientDeathDateAndTime().getTime().getValue()==null){}else{patient.setDeceasedTime(pid.getPatientDeathDateAndTime().getTime().getValueAsDate());}
		hibernateManager.update(patient);
	}
	
	public void updatePatientAddressDetails(){
		PID pid=adtMsg.getPID();
		XAD[] addressList=pid.getPatientAddress();
		for(int i=0;i<addressList.length;i++){
			pAddress=new PatientAddress();
			pAddress.setPatientId(this.patient.getPatientId());
			pAddress.setCity1(addressList[i].getOtherDesignation().getValue());
			pAddress.setCountry(addressList[i].getCountry().getValue());
			pAddress.setHouseNumber(addressList[i].getStreetAddress().getDwellingNumber().getValue());
			pAddress.setCity2(addressList[i].getCity().getValue());
			pAddress.setStreet(addressList[i].getStreetAddress().getStreetName().getValue());
			hibernateManager.saveObject(pAddress);
		}
	}
	
}
